package com.example.mainstream.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.myapp.activity.GoodsCarActivity.ClearCarThread;
import com.myapp.activity.GoodsCarActivity.DeleteCarItemThread;
import com.myapp.activity.GoodsCarActivity.GoodsCarListThread;
import com.myapp.adapter.GoodsCarListAdapter;
import com.myapp.adapter.OrderDetailListAdapter;
import com.myapp.common.HttpUtil;
import com.myweb.domain.GoodsCar;
import com.myweb.domain.Note;
import com.myweb.domain.UserOrder;
import com.myweb.domain.UserOrderDetail;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class OrderDetailActivity extends ActionBarActivity {

	TextView orderdetaillist_ordernum;

	TextView orderdetaillist_address;

	TextView orderdetaillist_recusername;

	TextView orderdetaillist_tel;

	TextView orderdetaillist_sendtype;

	ListView listView;

	private String orderid;

	List<Map<String, Object>> orderdetaillist1;

	private OrderDetailListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_order_detail_list);

		listView = (ListView) findViewById(R.id.orderdetail_list);// ��ȡ�б�Ԫ��

		orderdetaillist1 = new ArrayList<Map<String, Object>>();

		adapter = new OrderDetailListAdapter(this, orderdetaillist1);// ������

		listView.setAdapter(adapter);

		orderdetaillist_ordernum = (TextView) findViewById(R.id.orderdetaillist_ordernum);

		orderdetaillist_address = (TextView) findViewById(R.id.orderdetaillist_address);

		orderdetaillist_recusername = (TextView) findViewById(R.id.orderdetaillist_recusername);

		orderdetaillist_tel = (TextView) findViewById(R.id.orderdetaillist_tel);

		orderdetaillist_sendtype = (TextView) findViewById(R.id.orderdetaillist_sendtype);

		Intent intent = getIntent();

		orderid = intent.getStringExtra("id");
		
	 
		
		Thread orderDetailThread = new Thread(new OrderDetailThread());

		orderDetailThread.start();
		
	}
	
	//�鿴���ﳵ��Ʒ����
	public void ViewOrderGoodsDetail(String id) {

		Intent intent = new Intent();

		intent.putExtra("id", id);

		intent.setClass(OrderDetailActivity.this, OrderDetailGoodsListDetailActivity.class);

		// ����Activity
		startActivity(intent);
	}
	
	// ��ȡ�����Ƕ��߳�
	class OrderDetailThread implements Runnable {

		public void run() {
			try {

				JSONObject paraObj = new JSONObject();

				paraObj.put("orderid", orderid);

				paraObj.put("action", "detail");

				String result = HttpUtil.getJsonFromServlet(paraObj.toString(),
						"UserOrderService");

				if (!result.equals("")) {

					UserOrder order = JSONObject.parseObject(result,
							UserOrder.class);

					Map<String, Object> orderMap1 = new HashMap<String, Object>();

					orderMap1.put("ordernum", order.getOrdernum());

					orderMap1.put("address", order.getAddress());

					orderMap1.put("recusername", order.getRecusername());

					orderMap1.put("sendtype", order.getSendtype());

					orderMap1.put("tel", order.getTel());

					Message msg = new Message();

					msg.what = 1;// ���ʳɹ����з���ֵ

					msg.obj = orderMap1;

					handler.sendMessage(msg);

				}

			} catch (Exception ex) {

				System.out.println("ex: " + ex.getMessage());

				Message msg = new Message();

				msg.what = 0;// �����쳣

				handler.sendMessage(msg);

			}
		}
	}

	
	class OrderDetailListThread implements Runnable {

		public void run() {
			try {

				JSONObject paraObj = new JSONObject();

				paraObj.put("action", "detaillist");

				MyApplication myApp = (MyApplication) getApplication(); // ����Զ����Ӧ�ó���MyApp

				paraObj.put("orderid", orderid);

				String result = HttpUtil.getJsonFromServlet(paraObj.toString(),
						"UserOrderService");

				if (!result.equals("")) {

					List<UserOrderDetail> orderdetailList = JSONObject
							.parseArray(result, UserOrderDetail.class);

					ArrayList<Map<String, Object>> orderdetailListMap = new ArrayList<Map<String, Object>>();

					for (UserOrderDetail userorderdetail : orderdetailList) {

						Map<String, Object> map = new HashMap<String, Object>();

						map.put("id", userorderdetail.getGoodsid());

						String _name = userorderdetail.getGoodsname();

						if (_name.length() > 16) {
							_name = _name.substring(0, 16) + "...";
						}
						map.put("name", _name);

						map.put("buynum", userorderdetail.getBuynum());

						if (userorderdetail.getImgpath().length() > 0) {

							map.put("imgpath", HttpUtil.BASE_URL
									+ userorderdetail.getImgpath());
						} else {
							map.put("imgpath", "");
						}

						map.put("sumprice", userorderdetail.getSumprice());

						orderdetailListMap.add(map);
					}

					Message msg = new Message();

					msg.what = 0;// ���ʳɹ����з���ֵ

					msg.obj = orderdetailListMap;

					handler.sendMessage(msg);

				}

			} catch (Exception ex) {

				System.out.println("ex: " + ex.getMessage());

				Message msg = new Message();

				msg.what = 2;// �����쳣

				handler.sendMessage(msg);

			}
		}
	}

	// Handler
	Handler handler = new Handler() {

		public void handleMessage(Message msg) {

			super.handleMessage(msg);

			switch (msg.what) {

			case 0:
				try {
					ArrayList<Map<String, Object>> result = (ArrayList<Map<String, Object>>) msg.obj;

					if (result != null) {

						float sumprice = 0;

						orderdetaillist1.clear();

						orderdetaillist1.addAll(result);

						adapter.notifyDataSetChanged();

						System.out.println(result.toString());
					}
				} catch (Exception ex) {

					System.out.println("ex: " + ex.getMessage());
				}
				break;
			case 1:

				Map<String, Object> orderMap;

				try {
					orderMap = (Map<String, Object>) msg.obj;

					orderdetaillist_ordernum.setText(orderMap.get("ordernum")
							.toString());

					orderdetaillist_sendtype.setText(orderMap.get("sendtype")
							.toString());

					orderdetaillist_recusername.setText(orderMap.get(
							"recusername").toString());

					orderdetaillist_address.setText(orderMap.get("address")
							.toString());

					orderdetaillist_tel.setText(orderMap.get("tel").toString());
					
					Thread orderdetaillistThread = new Thread(new OrderDetailListThread());

					orderdetaillistThread.start();

				} catch (Exception ex) {
					System.out.print(ex.getMessage());
				}
				break;

			case 2:

				Toast.makeText(getApplicationContext(), "����ʧ�ܣ���������!",
						Toast.LENGTH_SHORT).show();
				break;
			}

		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.add_question, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	class ClickEvent implements OnClickListener {

		@Override
		public void onClick(View v) {
			v.getTag();
		}

	}

}
